package com.ccExcelUtil.validator.relatedField;


import com.ccExcelUtil.service.ValidatedRelatedFieldService;
import com.ccExcelUtil.util.ViolationUtils;
import com.ccExcelUtil.validator.ValidatorUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author xiaozhao
 */
public class RelatedClassValidator implements ConstraintValidator<RelatedClass, Object> {

    ViolationUtils validatorUtils;

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        validatorUtils = new ViolationUtils(context);
        ValidatedRelatedFieldService<Object> validatedRelatedFieldService = ValidatorUtils.VALIDATED_RELATED_FIELD_SERVICE_MAP.get(value.getClass().getName());
        if (validatedRelatedFieldService == null) {
            // 如果没有找到对应的服务，返回true（跳过校验）
            return true;
        }
        return validatedRelatedFieldService.validator(value);
    }
}
